/*
 *             Copyright (C) 2024 Steve Jrong
 *
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.user.config;

import com.stevejrong.waiter.common.component.DomainEventPublisher;
import jakarta.inject.Named;
import lombok.Data;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * 配置类 - 用户中心的通用生产者
 *
 * @author Steve Jrong
 * @since 1.0
 */
@Data
@Configuration
public class UserAppMQProducerConfig {

    @Value("${waiter.user.rocketmq.name-server}")
    private String nameServerAddr;

    @Value("${waiter.user.rocketmq.producer.group}")
    private String producerGroup;

    /**
     * 适用于用户中心的消息生产者
     *
     * @return 适用于用户中心的消息生产者Bean
     * @throws MQClientException 消息客户端创建异常
     */
    @Bean
    public MQProducer waiterUserAppProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup); // 创建消息生产者
        producer.setNamesrvAddr(nameServerAddr); // NameServer的地址
        producer.start(); // 启动生产者组
        return producer;
    }

    /**
     * 适用于用户中心的领域事件发布器
     *
     * @param waiterUserAppProducer 适用于用户中心的消息生产者Bean
     * @return 适用于用户中心的领域事件发布器Bean
     */
    @Bean
    @DependsOn("waiterUserAppProducer")
    public DomainEventPublisher userAppDomainEventPublisher(@Named("waiterUserAppProducer") MQProducer waiterUserAppProducer) {
        return new DomainEventPublisher(waiterUserAppProducer);
    }
}